Module Extend_Texp
USE POLYMORPHIC_COMPLEXTAYLOR
private explieflo

  INTERFACE texp
     MODULE PROCEDURE explieflo ! flow on maps
  END INTERFACE


contains 

  FUNCTION explieflo( t,S1, S2 )
    implicit none
    TYPE (damap) explieflo
    TYPE (vecfield), INTENT (IN) :: S1
    TYPE (damap), INTENT (IN) ::  S2
    TYPE (damap) tempnew,RESIDUE
    real(dp) :: eps =1.d-10
    TYPE (vecfield) f,FK
	real(dp), intent(in):: t
	integer i,localmaster,k
    real(dp) normb,norma


    localmaster=master 
    call assdamap(explieflo)  ! Assigned Scratch Variables and set master=master +1 ; master is the level of Scratch variables
    
	call alloc(tempnew,RESIDUE)
	call alloc(F,FK)
    
	
    do i=1,c_%nd2
	 f%v(i)=t*s1%v(i)
	enddo 
     
    select case(S1%ifac) 

	case(0)
    tempnew=S2
    explieflo=S2
    normb=1.e30_dp
		do i=1,1000
         tempnew= (1.0_dp/i)*(F*tempnew)
		 RESIDUE=EXPLIEFLO
         explieflo=explieflo + tempnew
         RESIDUE=RESIDUE-explieflo
		 norma=full_abs(residue)
		 if(i>c_%no) then
		  if(norma>=normb) then
		    exit
		  else
		    normb=norma
		  endif
		 endif
		enddo
    case(1)
    tempnew=S2
    explieflo=S2
	do k=c_%no,0,-1  ! including F_1 and F_0
    normb=1.e30_dp
    tempnew=explieflo
	FK=F.SUB.K
	FK%ifac=0
		do i=1,1000
         tempnew= (1.0_dp/i)*(FK*tempnew)
		 RESIDUE=EXPLIEFLO
         explieflo=explieflo + tempnew
         RESIDUE=RESIDUE-explieflo
		 norma=full_abs(residue)
		 if(i>c_%no) then
		  if(norma>=normb) then
		    exit
		  else
		    normb=norma
		  endif
		 endif
		enddo
    enddo
	case(-1)
    tempnew=S2
    explieflo=S2
	do k=0,c_%no  ! including F_1 and F_0
    normb=1.e30_dp
    tempnew=explieflo
	FK=F.SUB.K
	FK%ifac=0
		do i=1,1000
         tempnew= (1.0_dp/i)*(FK*tempnew)
		 RESIDUE=EXPLIEFLO
         explieflo=explieflo + tempnew
         RESIDUE=RESIDUE-explieflo
		 norma=full_abs(residue)
		 if(i>c_%no) then
		  if(norma>=normb) then
		    exit
		  else
		    normb=norma
		  endif
		 endif
		enddo
    enddo

	end select

    master=localmaster
    call kill(tempnew,RESIDUE)
    call kill(F,FK)

  END FUNCTION explieflo


end Module Extend_Texp